<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/generalFunctions.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/stringUtil.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/controller/ctlAbstract.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/user.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/resource.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busQuick_Message.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAddress.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busOccupation.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busAvatar.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busProvider.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busItem.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busFriend.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busCategory.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busMessage.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busResource.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUpdate.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busLocation.php';

class LoadController extends AbstractController {
	private $_userBus;
	private $_categoryBus;
	private $_quickMessageBus;
	private $_friendBus;
	private $_avatarBus;
	private $_occupationBus;
	private $_providerBus;
	private $_itemBus;
	private $_messageBus;
	private $_resourceBus;
	private $_updateBus;
	private $_locationBus;
		
	protected function getUser($getVars) {
		$user = null;

		if (isset($getVars['userid'])) {
			$user = $this->_userBus->getUser($getVars['userid']);
		} else {
			$user = $this->_userBus->getUserByUserName($getVars['username']);
		}
		
		return $user;
	}
		
	public function main(array $getVars) {
		global $ubi;
		
		$this->_userBus = new UserBus();
		$this->_categoryBus = new CategoryBus();
		$this->_quickMessageBus = new Quick_MessageBus();
		$this->_friendBus = new FriendBus();
		$this->_avatarBus = new AvatarBus();
		$this->_occupationBus = new OccupationBus();
		$this->_providerBus = new ProviderBus();
		$this->_itemBus = new ItemBus();
		$this->_messageBus = new MessageBus();
		$this->_resourceBus = new ResourceBus();
		$this->_updateBus = new UpdateBus();
		$this->_locationBus = new LocationBus();
		
		$userID = null; 
		if (isset($_SESSION['userid'])) {
			$userID = $_SESSION['userid'];	
		}
				
		if ($getVars['view'] == 'header') {
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/template/header.php';
		} else if ($getVars['view'] == 'info') {
			if (isset($_COOKIE[$ubi['SITE']['IP_REGION_NAME']])) {
				echo $_COOKIE[$ubi['SITE']['IP_REGION_NAME']];
				return;
			}
			
			$ip = IPInformation::getIPInformation();
			if (isset($ip) && $ip != '') {
				setcookie($ubi['SITE']['IP_COOKIENAME'], $ip);
			}
			if (!isset($ip) && $ip == '' && isset($_COOKIE[$ubi['SITE']['IP_COOKIENAME']])) {
				$ip = $_COOKIE[$ubi['SITE']['IP_COOKIENAME']]; 
			}
			
			if (isset($ip) && $ip != '') {
				IPInformation::getRegionInformation($ubi['SITE']['IP_SITE'] . $ip, $latitude, $longitude);
				setcookie($ubi['SITE']['IP_LATITUDE_COOKIENAME'], $latitude);
				setcookie($ubi['SITE']['IP_LONGITUDE_COOKIENAME'], $longitude);
			} 
			$location = $this->_locationBus->getLocationNearest($latitude, $longitude);
							
			if (isset($location)) {
				setcookie($ubi['SITE']['IP_REGION_NAME'], $location->getName());
				echo $location->getName();
			}			
		} else if ($getVars['view'] == 'inputItem') {
			$arrCatItem1 = $this->_categoryBus->getCategoryItemList(0);
			$catItem1 = $arrCatItem1[0];							
			$arrCatItem2 = $this->_categoryBus->getCategoryItemList($catItem1->getCategoryID());
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewInputItem.php';
		} else if ($getVars['view'] == 'inputProvider') {
			$city = StringUtil::getCityName($_POST['city']);
			$district = StringUtil::getDistrictName($_POST['district']);
			$street = $_POST['street'];
			$ward = StringUtil::getWardName($_POST['ward']);
			
			$arrLocation = $this->_locationBus->getAddressInfoBaseOnDB(array($city, $district, $ward));
			 
			$latitude = $_POST['latitude'];
			$longitude = $_POST['longitude'];
			
			$city = $arrLocation[0]['id'];
			$district = $arrLocation[1]['id'];
			$ward = $arrLocation[2]['id'];
			
			$arrLocationCity = $this->_locationBus->getLocationList(null);
			$arrLocationDistrict = $this->_locationBus->getLocationList($city);
			$arrLocationWard = $this->_locationBus->getLocationList($district);
			
			$arrCatProvider1 = $this->_categoryBus->getCategoryProviderList(0);
			$catProvider1 = $arrCatProvider1[0];										
			$arrCatProvider2 = $this->_categoryBus->getCategoryProviderList($catProvider1->getCategoryID());

			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewInputProvider.php';
		} else if ($getVars['view'] == 'profileInformation') {
			$user = $this->_userBus->getUser($getVars['userid']);
			
			$resource = $this->_avatarBus->getResource($user->getUserID());
			$occupation = $this->_occupationBus->getOccupation($user->getOccupationID());
			$favItemList = $this->_userBus->getFavouriteItems($user->getUserID());
			$favProviderList = $this->_userBus->getFavouriteProviders($user->getUserID());
			
			$nNewMessages = $this->_messageBus->countUnreadmessageByReceiverId($user->getUserID());
			if ($userID) {
				$friendStatus = $this->_friendBus->getStatus($userID, $getVars['userid']);
			}
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewProfileInformation.php';
		} else if ($getVars['view'] == 'quickMessages')	{
			$user = $this->getUser($getVars);
			$quick_messageBus = new Quick_MessageBus();
						
			$quick_messages = $this->_quickMessageBus->getTopQuick_MessagesByReceiverId($user->getUserID());
			
			//create a list of userid that write quick messages
			$idList = array();
			
			for ($i = 0; $i < count($quick_messages); $i++) {
				$id = $quick_messages[$i]->getSenderID();
				$exist = false;
				for ($j = 0; $j < count($idList); $j++) {
					if ($idList[$j] == $id) {
						$exist = true;
						break;
					}
				}
				
				if ($exist == false) {
					$idList[] = $id;
				}
			}
			$usersWrote = $this->_userBus->getUserListByUserIdList($idList);
			$imgs = $this->_avatarBus->getAvatarPathOfUsers($usersWrote);
						 
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewQuickMessages.php';
		} else if ($getVars['view'] == 'userControl') {
			$user = $this->getUser($getVars);
			
			$isMine = $this->isMine($user);
			
			$isFriend = false;
			$busFriend = new FriendBus();
				
			if ($userID != null) {
				$isFriend = $busFriend->isFriend($userID, $user->getUserID());
				$nUnreadMessage = $this->_messageBus->countUnreadmessageByReceiverId($getVars['userid']);
			}
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewUserControl.php';
		} else if ($getVars['view'] == 'userBlast') {
			$user = $this->getUser($getVars);
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewUserBlast.php';
		} else if ($getVars['view'] == 'sendQuickMessage') {
			if ($userID != null) {
				$user = $this->getUser($getVars);

				require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewSendQuickMessage.php';
			}
		} else if ($getVars['view'] == 'friends') {
			$user = $this->_userBus->getUser($getVars['userid']);
			$fList = $this->_friendBus->getFriendsByUserId($user->getUserID());
			$idList = array();
			for ($i = 0; $i < count($fList); $i++) {
				$idList[] = $fList[$i]->getFriendID();
			}
			
			$friendList = $this->_userBus->getUserListByUserIdList($idList);
			
			$imgs = $this->_avatarBus->getAvatarPathOfUsers($friendList);
						
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewFriends.php';
		} else if ($getVars['view'] == 'favouriteItems') {
			$user = $this->getUser($getVars);
			
			$favList = $this->_userBus->getFavouriteItems($user->getUserID());
			
			$interactType = $ubi['ITEM']['VALUE'];
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewFavourite.php';
		}  else if ($getVars['view'] == 'favouriteProviders') {
			$user = $this->getUser($getVars);
			
			$favList = $this->_userBus->getFavouriteProviders($user->getUserID());
			
			$interactType = $ubi['PROVIDER']['VALUE'];
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewFavourite.php';
		} else if ($getVars['view'] == 'blockSignIn') {
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewSignIn.php';
		} else if ($getVars['view'] == 'blockSearch') {
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewSearch.php';
		} else if ($getVars['view'] == 'blockMembers') {
			$nUser = $this->_userBus->getNumberOfUser();
			$users = $this->_userBus->getBestUsers();
			$imgs = $this->_avatarBus->getAvatarPathOfUsers($users);
						
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewMembers.php';
		} else if ($getVars['view'] == 'blockNewestPosts') {
			$providers = $this->_providerBus->getNewestProviders();
			$imgProviders = array();
			foreach ($providers as $provider) {
				$arrResID = explode (' ', trim($provider->getResourceList()));
				$imgProviders[$provider->getProviderID()] = $this->_resourceBus->getImgOfResource($arrResID[0]);	
			}
			
			$items = $this->_itemBus->getNewestItems();
			$imgItems = array();
			foreach ($items as $item) {
				$arrResID = explode(' ', trim($item->getResourceList()));
				$imgItems[$item->getItemID()] = $this->_resourceBus->getImgOfResource($arrResID[0]);	
			}
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewNewestPosts.php';
		} else if ($getVars['view'] == 'menu') {
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewMenu.php';
		} else if ($getVars['view'] == 'highestProvider') {
			$obj = $this->_providerBus->getProviderMaxFrequency();
			if (isset($obj) && ($obj->getProviderID() != null)) {
				$arrResID = explode(' ', trim($obj->getResourceList()));
				$imgObj = $this->_resourceBus->getImgOfResource($arrResID[0]);
				$userObj = $this->_userBus->getUser($obj->getUserID());
				$resImg = $this->_avatarBus->getResource($userObj->getUserID());
				if (isset($resImg)) {
					$imgUserObj = $resImg->getResourceName();
				}
				$numCommentObj = $this->_providerBus->getNumberOfComment($obj->getProviderID());
				$numFavObj = $this->_providerBus->getNumberOfFavourites($obj->getProviderID());
				
			
				require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewHighest.php';
			}
		} else if ($getVars['view'] == 'highestItem') {
			$obj = $this->_itemBus->getItemMaxFrequency();
			if (isset($obj) && ($obj->getItemID() != null)){
				$arrResID = explode(' ', trim($obj->getResourceList()));		
				$imgObj = $this->_resourceBus->getImgOfResource($arrResID[0]);
				$userObj = $this->_userBus->getUser($obj->getUserID());
				$resImg = $this->_avatarBus->getResource($userObj->getUserID());
				if (isset($resImg)) {
					$imgUserObj = $resImg->getResourceName();
				}
				$numCommentObj = $this->_itemBus->getNumberOfComment($obj->getItemID());
				$numFavObj = $this->_itemBus->getNumberOfFavourites($obj->getItemID());
			
				require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewHighest.php';
			}
		} else if ($getVars['view'] == 'footer') {
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewFooter.php';
		} else if ($getVars['view'] == 'randomItemProvider') {
			$randomProviders = $this->_providerBus->getRandomProviders();
			$imgProviders = array();
			
			foreach ($randomProviders as $provider) {
				$arrResID = explode (' ', trim($provider->getResourceList()));
				$imgProviders[$provider->getProviderID()] = $this->_resourceBus->getImgOfResource($arrResID[0]);	
			}
			
			$randomItems = $this->_itemBus->getRandomItems();
			$imgItems = array();
			foreach ($randomItems as $item) {
				$arrResID = explode (' ', trim($item->getResourceList()));
				$imgItems[$item->getItemID()] = $this->_resourceBus->getImgOfResource($arrResID[0]);	
			}
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewRandomItemProvider.php';
		} else if ($getVars['view'] == 'userProviderPost') {
			if (!isset($getVars['page'])) {
				$getVars['page'] = 1;	
			}
			
			$uid = $getVars['userid'];
			
			$numProviderPost = $this->_providerBus->getNumberOfProvidersByUserID($uid);
			$numberOfProviderPages = GeneralFunctions::calculateNumberOfPages($numProviderPost, $ubi['SITE']['USER_NUMBER_POST']);
			$providers = $this->_providerBus->getProvidersByUserIDOfPage($uid, $getVars['page']);
			$imgProviders = array();
			
			foreach ($providers as $provider) {
				$arrResID = explode(' ', trim($provider->getResourceList()));
				$imgProviders[$provider->getProviderID()] = $this->_resourceBus->getImgOfResource($arrResID[0]);	
			}

			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewUserPostProvider.php';
		} else if ($getVars['view'] == 'userItemPost') {
			if (!isset($getVars['page'])) {
				$getVars['page'] = 1;	
			}
			
			$uid = $getVars['userid'];
			$numItemPost = $this->_itemBus->getNumberOfItemsByUserId($uid);
			$numberOfItemPages = GeneralFunctions::calculateNumberOfPages($numItemPost, $ubi['SITE']['USER_NUMBER_POST']);
			$items = $this->_itemBus->getItemsByUserIDOfPage($uid, $getVars['page']);
			
			$imgItems = array();
			
			foreach ($items as $item) {
				$arrResID = explode(' ', trim($item->getResourceList()));
				$imgItems[$item->getItemID()] = $this->_resourceBus->getImgOfResource($arrResID[0]);	
			}

			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewUserPostItem.php';
		} else if ($getVars['view'] == 'updateOwner') {
			$uid = $getVars['userid'];
			$updates = $this->_updateBus->getUpdatesOfUser($uid);
			
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewUserUpdate.php';
		} else if ($getVars['view'] == 'updateFriends') {
			$uid = $getVars['userid'];
			$updates = $this->_updateBus->getUpdatesOfFriends($uid);
			$friends = array();
			if ($updates) {
				foreach ($updates as $update) {
					$friends[$update->getUserID()] = $this->_userBus->getUser($update->getUserID());
				}
			}
			require_once $ubi['SERVER']['SERVER_ROOT'] . '/view/viewFriendsUpdate.php';
		}
	}
}
?>